package com.centaline.androidsalesblog.rx.request;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.api.usercenter.ReservationListApi;
import com.centaline.androidsalesblog.model.responseobject.ReservationRo;
import com.centanet.framework.interfaces.IResponseListener;

import rx.Observable;
import rx.Subscriber;

/**
 * 未约看数量
 * Created by guilin on 16/5/12.
 */
final class RequestReserveCountOnSubscribe implements Observable.OnSubscribe<ReservationRo> {

    private final Class<?> tag;

    public RequestReserveCountOnSubscribe(Class<?> tag) {
        this.tag = tag;
    }

    @Override
    public void call(final Subscriber<? super ReservationRo> subscriber) {
        ReservationListApi reservationListApi = new ReservationListApi(tag,
                ReservationRo.class,
                new IResponseListener<ReservationRo>() {
                    @Override
                    public void onResponse(ReservationRo reservationRo) {
                        if (!subscriber.isUnsubscribed()) {
                            subscriber.onNext(reservationRo);
                        }
                        subscriber.onCompleted();
                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        subscriber.onError(error);
                    }
                });
        reservationListApi.setReserve(false);
        reservationListApi.setCount(0);
        reservationListApi.startRequest();
    }
}
